package com.manage.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.manage.common.filter.JwtUser;
import com.manage.dao.CmSysUserMapper;
import com.manage.pojo.entity.CmSysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private CmSysUserMapper cmSysUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        CmSysUser user = cmSysUserMapper.selectOne(
                Wrappers.<CmSysUser>lambdaQuery()
                        .eq(CmSysUser::getUsername, username)
                        .isNull(CmSysUser::getDeleteTime)
        );        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return new JwtUser(user);
    }
}
